package com.daocao.hua.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.daocao.hua.R;
import com.daocao.hua.api.callback.AddressContract;
import com.daocao.hua.api.requestInfo.AddressRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.model.AddressModel;
import com.daocao.hua.util.DisplayUtil;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.ScacheUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.UI;
import com.daocao.hua.view.CircularProgress;
import com.lljjcoder.citypickerview.widget.CityPicker;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.OnAddressSelectedListener;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;

/**
 * Created by xsooy-pc on 2017/7/5.
 */

public class AddressActivity extends BaseActivity implements AddressContract.View, OnAddressSelectedListener {
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.txt_area)
    EditText txtArea;
    @BindView(R.id.edit_address)
    EditText editAddress;
    @BindView(R.id.progressBar)
    CircularProgress progressBar;
    //    private CityPicker cityPicker;
    private Unbinder unbinder;
    private AddressRequest api;
    private String province;
    private String city;
    private String area;
    private BottomDialog bottomDialog;
    private AddressModel.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_address);
        baseInitView(this, "AddressActivity", Const.ViewType.ACTIVITY);
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void showLoading(boolean b) {
        if (progressBar != null) {
            if (b)
                progressBar.setVisibility(View.VISIBLE);
            else
                progressBar.setVisibility(View.GONE);
        }
    }

    private void init() {
        setBackIntent();
        setTitleText("收货地址");
        bottomDialog = new BottomDialog(this);
        bottomDialog.setOnAddressSelectedListener(this);
        api = AddressRequest.create();
        api.attachView(this);
        api.getAddress();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bottomDialog != null) {
            bottomDialog.dismiss();
            bottomDialog = null;
        }
        ScacheUtil.clearTextLineCache();
        unbinder.unbind();
        unbinder = null;
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getAddressSuccess(AddressModel item) {
        data = item.data;
        if (data != null) {
//            editName.setText(data.name);
//            editPhone.setText(data.phone);
            editAddress.setText(data.address);
            txtArea.setText(data.province + data.city + data.area);
        }

    }

    @Override
    public void updateAddressSuccess(AddressModel item) {
        PreferenceUtils.putString(this, "address_city", item.data.province + item.data.city + item.data.area);
        ToastUtil.makeText(this, "修改成功").show();
    }

    @OnClick({R.id.txt_area, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_area:
                bottomDialog.show();
                break;
            case R.id.btn_save:
                if (checkCity() && checkAddress()) {
                    if (data != null && TextUtils.isEmpty(data.name)) {
                        api.updateAddress(data.name, UI.toString(editAddress), PreferenceUtils.getString(this, "mobile", ""), province, city, area);
                    } else if (!TextUtils.isEmpty(PreferenceUtils.getString(this, "real_name", ""))) {
                        if(TextUtils.isEmpty(province)){
                            api.updateAddress(PreferenceUtils.getString(this, "real_name", ""), UI.toString(editAddress), PreferenceUtils.getString(this, "mobile", ""), data.province, data.city, data.area);
                        }else {
                            api.updateAddress(PreferenceUtils.getString(this, "real_name", ""), UI.toString(editAddress), PreferenceUtils.getString(this, "mobile", ""), province, city, area);
                        }
                    } else if (!TextUtils.isEmpty(PreferenceUtils.getString(this, "username", ""))) {
                        if(TextUtils.isEmpty(province)){
                            api.updateAddress(PreferenceUtils.getString(this, "username", ""), UI.toString(editAddress), PreferenceUtils.getString(this, "mobile", ""), data.province, data.city, data.area);
                        }else {
                            api.updateAddress(PreferenceUtils.getString(this, "username", ""), UI.toString(editAddress), PreferenceUtils.getString(this, "mobile", ""), province, city, area);
                        }
                    }
//                    api.updateAddress(UI.toString(editName), UI.toString(editAddress), UI.toString(editPhone), province, city, area);
                }

                break;
        }
    }

    private boolean checkAll() {
        return checkName() && checkPhone() && checkCity() && checkAddress();
    }

    private boolean checkName() {
        if (TextUtils.isEmpty(UI.toString(editName))) {
            ToastUtil.makeText(this, "姓名不能为空").show();
            return false;
        }
        return true;
    }

    private boolean checkPhone() {
        if (TextUtils.isEmpty(UI.toString(editPhone))) {
            ToastUtil.makeText(this, "收货电话不能为空").show();
            return false;
        }
        return true;
    }

    private boolean checkCity() {
        if (TextUtils.isEmpty(UI.toString(txtArea))) {
            ToastUtil.makeText(this, "所在地区不能为空").show();
            return false;
        }
        return true;
    }

    private boolean checkAddress() {
        if (TextUtils.isEmpty(UI.toString(editAddress))) {
            ToastUtil.makeText(this, "详细地址不能为空").show();
            return false;
        }
        return true;
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        String s =
                (province == null ? "" : province.name) +
                        (city == null ? "" : city.name) +
                        (county == null ? "" : county.name) +
                        (street == null ? "" : street.name);

        if (province != null) {
            this.province = province.name;
        }
        if (city != null) {
            this.city = city.name;
        }
        if (county != null) {
            if (street != null) {
                area = county.name + street.name;
            } else {
                area = county.name;
            }
        }
        txtArea.setText(s);
        bottomDialog.dismiss();
    }
}
